package cn.iocoder.yudao.module.agri.service.farmmodelstage;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.agri.controller.admin.farmmodelstage.vo.*;
import cn.iocoder.yudao.module.agri.dal.dataobject.farmmodelstage.FarmModelStageDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.agri.convert.farmmodelstage.FarmModelStageConvert;
import cn.iocoder.yudao.module.agri.dal.mysql.farmmodelstage.FarmModelStageMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.agri.enums.ErrorCodeConstants.*;

/**
 * 种养模型-生长阶段 Service 实现类
 *
 * @author 广东小蜂
 */
@Service
@Validated
public class FarmModelStageServiceImpl implements FarmModelStageService {

    @Resource
    private FarmModelStageMapper farmModelStageMapper;

    @Override
    public Long createFarmModelStage(FarmModelStageCreateReqVO createReqVO) {
        // 插入
        FarmModelStageDO farmModelStage = FarmModelStageConvert.INSTANCE.convert(createReqVO);
        farmModelStageMapper.insert(farmModelStage);
        // 返回
        return farmModelStage.getId();
    }

    @Override
    public void updateFarmModelStage(FarmModelStageUpdateReqVO updateReqVO) {
        // 校验存在
        validateFarmModelStageExists(updateReqVO.getId());
        // 更新
        FarmModelStageDO updateObj = FarmModelStageConvert.INSTANCE.convert(updateReqVO);
        farmModelStageMapper.updateById(updateObj);
    }

    @Override
    public void deleteFarmModelStage(Long id) {
        // 校验存在
        validateFarmModelStageExists(id);
        // 删除
        farmModelStageMapper.deleteById(id);
    }

    private void validateFarmModelStageExists(Long id) {
        if (farmModelStageMapper.selectById(id) == null) {
            throw exception(FARM_MODEL_STAGE_NOT_EXISTS);
        }
    }

    @Override
    public FarmModelStageDO getFarmModelStage(Long id) {
        return farmModelStageMapper.selectById(id);
    }

    @Override
    public List<FarmModelStageDO> getFarmModelStageList(Collection<Long> ids) {
        return farmModelStageMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<FarmModelStageDO> getFarmModelStagePage(FarmModelStagePageReqVO pageReqVO) {
        return farmModelStageMapper.selectPage(pageReqVO);
    }

    @Override
    public List<FarmModelStageDO> getFarmModelStageList(FarmModelStageExportReqVO exportReqVO) {
        return farmModelStageMapper.selectList(exportReqVO);
    }

}
